<?php

class viewEmploymentContractListAction extends baseTLUPimAction
{
    private $employmentContractService;
    private $employmentContractTypeService;

    public function getEmploymentContractService()
    {
        if (is_null($this->employmentContractService)) {
            $this->employmentContractService = new EmploymentContractService();
            $this->employmentContractService->setEmploymentContractDao(new EmploymentContractDao());
        }
        return $this->employmentContractService;
    }

    public function setEmploymentContractService(EmploymentContractService $employmentContractService)
    {
        $this->employmentContractService = $employmentContractService;
    }

    public function getEmploymentContractTypeService()
    {
        if (is_null($this->employmentContractTypeService)) {
            $this->employmentContractTypeService = new EmploymentContractTypeService();
            $this->employmentContractTypeService->setEmploymentContractTypeDao(new EmploymentContractTypeDao());
        }
        return $this->employmentContractTypeService;
    }

    public function setForm(sfForm $form)
    {
        if (is_null($this->form)) {
            $this->form = $form;
        }
    }

    public function execute($request)
    {
        $empNumber = $request->getParameter('empNumber');
        $this->empNumber = $empNumber;

        $this->employmentContractPermissions = $this->getDataGroupPermissions('employment-contract', $empNumber);

        if (!$this->IsActionAccessible($empNumber)) {
            $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
        }

        $this->employmentContractTypes = $this->getEmploymentContractTypeList();

        $this->sorter = new ListSorter('property.sort', 'tlupim_module', $this->getUser(), array('field_num', ListSorter::ASCENDING));

        $sortBy = 'start_date';
        $sortOrder = 'ASC';

        if ($request->getParameter('sort')) {
            $sortBy = $request->getParameter('sort');
            $sortOrder = $request->getParameter('order');
            $this->sortField = $sortBy;
            $this->sortOrder = $sortOrder;
        }

        $this->sorter->setSort(array($sortBy, $sortOrder));
        $this->employmentContracts = $this->getEmploymentContractService()->getEmploymentContractListByEmployeeNumber($this->empNumber, $sortBy, $sortOrder);
    }

    private function getEmploymentContractTypeList()
    {
        $employmentContractTypes = $this->getEmploymentContractTypeService()->getEmploymentContractTypeList("", "", false);

        $list = array();

        foreach ($employmentContractTypes as $employmentContractType) {
            $list[$employmentContractType->getId()] = ($employmentContractType->getIsDeleted() == EmploymentContractType::DELETED) ? $employmentContractType->getCode() . " (" . __("Deleted") . ")" : $employmentContractType->getCode();
        }

        return $list;
    }

}

